//
//  SYComposeToolBar.swift
//  YANAWeibo
//
//  Created by mac on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum SYComposeToolBarButtonType: Int {
    case Picture = 0
    case Mention = 1
    case Trend = 2
    case Emoticon = 3
    case Add = 4
}


class SYComposeToolBar: UIStackView {
    // 设置闭包
    var didSeletedButtonClosure: ((type: SYComposeToolBarButtonType)->())?
    
    /// 表情按钮
    var emoticonButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 指定布局方式
        axis = .Horizontal
        // 填充方式
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    }
    
    private func addChildButton(imageName: String, type: SYComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        // 获取枚举原始值
        button.tag = type.rawValue
        // 监听点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        // 去掉高亮
        button.adjustsImageWhenHighlighted = false
        addArrangedSubview(button)
        return button
    }
    
    func showEmoticonIcon(isEmoticon: Bool) {
        var icon = "compose_emoticonbutton_background"
        if isEmoticon {
            icon = "compose_keyboardbutton_background"
        }
        emoticonButton?.setImage(UIImage(named: icon), forState: .Normal)
        emoticonButton?.setImage(UIImage(named: "\(icon)__highlighted"), forState: .Highlighted)
    }
    
    @objc private func buttonClick(button: UIButton) {
        let type = SYComposeToolBarButtonType(rawValue: button.tag)!
        didSeletedButtonClosure?(type: type)
    }

}
